package com.library_management.model;

import java.io.Serializable;
import java.util.Date;

public class Book implements Serializable {
    // 1. 私有属性（对应数据库表字段，驼峰命名）
    private Integer id;
    private String bookname;
    private String author;
    private String isbn;
    private String publisher;
    private Date publishDate; // 对应表中 publish_date
    private Integer stock; // 总库存
    private Integer available; // 可借数量

    // 2. 无参构造器
    public Book() {}

    // 3. getter 和 setter 方法（含验证逻辑）
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id; // id 可空
    }

    public String getBookname() {
        return bookname;
    }

    public void setBookname(String bookname) {
        // 验证：非空
        if (bookname == null || bookname.trim().isEmpty()) {
            throw new IllegalArgumentException("bookname 不能为空");
        }
        this.bookname = bookname;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        // 验证：非空
        if (author == null || author.trim().isEmpty()) {
            throw new IllegalArgumentException("author 不能为空");
        }
        this.author = author;
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        // 验证：非空且长度13位
        if (isbn == null || isbn.length() != 13) {
            throw new IllegalArgumentException("isbn 必须非空且长度为13位");
        }
        this.isbn = isbn;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher; // 出版社可空
    }

    public Date getPublishDate() {
        return publishDate;
    }

    public void setPublishDate(Date publishDate) {
        this.publishDate = publishDate; // 出版日期可空
    }

    public Integer getStock() {
        return stock;
    }

    public void setStock(Integer stock) {
        // 验证：不能为负数
        if (stock != null && stock < 0) {
            throw new IllegalArgumentException("stock 不能为负数");
        }
        this.stock = stock;
    }

    public Integer getAvailable() {
        return available;
    }

    public void setAvailable(Integer available) {
        // 验证：不能为负数，且不能大于 stock（需先设置 stock 再设置 available）
        if (available != null) {
            if (available < 0) {
                throw new IllegalArgumentException("available 不能为负数");
            }
            if (this.stock != null && available > this.stock) {
                throw new IllegalArgumentException("available 不能大于 stock");
            }
        }
        this.available = available;
    }

    // 4. 重写 toString()，包含核心属性
    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", bookname='" + bookname + '\'' +
                ", author='" + author + '\'' +
                ", stock=" + stock +
                ", available=" + available +
                '}';
    }
}